package com.eusoft.grades.school;
/**
 * @author Nick Eubanks
 * 
 * Copyright (C) 2010 Android Infinity (http://www.androidinfinity.com)
 *
 */
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;

public class Category implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5032454515729155639L;
	public int drops = 0;
	public String ID = "", name = "";
	public ArrayList<Item> items = new ArrayList<Item>();

	public double weight;

	/**
	 * Returns grade of each item divided by total number of items (in percent)
	 * Adds each grade of each Item in @item and divides by @item .size() !Does
	 * not take weight of this category into account (handled in Course) ONLY
	 * calculates grade for items that are complete
	 * */
	public double calculateGrade() {

		double grade = 0;

		ArrayList<Double> itemGrades = new ArrayList<Double>();

		// extraCredit
		if (ID.equals("extracredit")) {
			for (Item i : items) {
				if (i.isGraded())
					itemGrades.add(new Double(i.getExtraCreditValue()));
			}

			itemGrades.trimToSize();// just in case

			for (int i = 0; i < itemGrades.size(); i++) {
				grade += itemGrades.get(i);
			}

			if (itemGrades.size() > 0)
				return grade;
			else
				return 0;
		}
		// not extra Credit
		else {

			for (Item i : items) {
				if (i.isGraded())
					itemGrades.add(new Double(i.calculateGrade()));
			}

			Collections.sort(itemGrades); // grades sorted from least to
											// greatest

			int tempDrops = drops;

			while (!itemGrades.isEmpty() && tempDrops > 0) {
				itemGrades.remove(0);
				tempDrops--;
			}
			itemGrades.trimToSize();// just in case

			for (int i = 0; i < itemGrades.size(); i++) {
				grade += itemGrades.get(i);
			}

			if (itemGrades.size() > 0)
				return grade / itemGrades.size();
			else
				return 0;
		}
	}

	public PointsObject getPointsEarned() {
		double grade = 0;

		ArrayList<PointsObject> itemGrades = new ArrayList<PointsObject>();

		// extraCredit
		if (ID.equals("extracredit")) {
			for (Item i : items) {
				if (i.isGraded())
					itemGrades.add(new PointsObject(i.pointsEarned,
							i.pointsPossible));
			}

			itemGrades.trimToSize();// just in case

			for (int i = 0; i < itemGrades.size(); i++) {
				grade += itemGrades.get(i).earned;
			}

			return new PointsObject(grade, 0);
		}
		// not extra Credit
		else {

			for (Item i : items) {
				if (i.isGraded())
					itemGrades.add(new PointsObject(i.pointsEarned,
							i.pointsPossible));
			}

			Collections.sort(itemGrades); // grades sorted from least to
											// greatest

			double earned = 0;
			double worth = 0;
			int tempDrops = drops;

			while (!itemGrades.isEmpty() && tempDrops > 0) {
				itemGrades.remove(0);
				tempDrops--;
			}
			itemGrades.trimToSize();// just in case

			for (PointsObject o : itemGrades) {
				earned += o.earned;
				worth += o.worth;
			}

			return new PointsObject(earned, worth);
		}
	}

	public boolean isExtraCredit() {
		if (ID.equals("extracredit"))
			return true;
		else
			return false;
	}
}

/**
 * 
 * @author Nick Eubanks
 */
class ItemObject {
	public double earned;
	public double worth;

	public ItemObject(double e, double w) {
		this.earned = e;
		this.worth = w;
	}

}
